package com.btshop.goods.controller;
import com.btshop.goods.pojo.Category;
import com.btshop.goods.service.CategoryService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: CategoryController
 * @description: Category 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "CategoryController")
@Slf4j
@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点id查询子分类
     * @param pid
     * @return
     */
    @ApiOperation(value = "Category根据父节点id查询子分类",notes = "根据父节点id查询子分类",tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "pid", value = "父节点id", required = true, dataType = "Long"),
    })
    @GetMapping("/list/{pid}")
    public Result<List<Category>> findByParentId(@PathVariable("pid")Long pid){
        List<Category> categories = categoryService.findByParentId(pid);
        return Result.buildSuccess("查询子分类成功！",categories);
    }

    /***
     * Category分页条件搜索实现
     * @param category
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Category条件分页查询",notes = "分页条件查询Category方法详情",tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Category对象",value = "传入JSON数据",required = false) Category category, @PathVariable  int page, @PathVariable  int size){
        log.info("CategoryController findPage params , category : {} , page : {} , size : {}",category,page,size);
        //调用CategoryService实现分页条件查询Category
        PageInfo<Category> pageInfo = categoryService.findPage(category, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Category分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Category分页查询",notes = "分页查询Category方法详情",tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("CategoryController findPage params , page : {} , size : {}",page,size);
        //调用CategoryService实现分页查询Category
        PageInfo<Category> pageInfo = categoryService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param category
     * @return
     */
    @ApiOperation(value = "Category条件查询",notes = "条件查询Category方法详情",tags = {"CategoryController"})
    @PostMapping(value = "/search" )
    public Result<List<Category>> findList(@RequestBody(required = false) @ApiParam(name = "Category对象",value = "传入JSON数据",required = false) Category category){
        log.info("CategoryController findList params , category : {} ",category);
        //调用CategoryService实现条件查询Category
        List<Category> list = categoryService.findList(category);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Category根据主键id删除",notes = "根据主键删除Category方法详情",tags = {"CategoryController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("CategoryController delete params ,   id : {}   ",id);
        //调用CategoryService实现根据主键删除
        categoryService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Category数据
     * @param category
     * @param id
     * @return
     */
    @ApiOperation(value = "Category根据id修改",notes = "根据ID修改Category方法详情",tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Category对象",value = "传入JSON数据",required = false) Category category,@PathVariable("id") Long id){
        log.info("CategoryController update params ,  category : {},  id : {}   ",category,id);
        //设置主键值
        category.setId(id);
        //调用CategoryService实现修改Category
        categoryService.update(category);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Category数据
     * @param category
     * @return
     */
    @ApiOperation(value = "Category添加",notes = "添加Category方法详情",tags = {"CategoryController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Category对象",value = "传入JSON数据",required = true) Category category){
        log.info("CategoryController add params , category : {}",category);
        //调用CategoryService实现添加Category
        categoryService.add(category);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Category根据id查询",notes = "根据ID查询Category方法详情",tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable("id") Long id){
        log.info("CategoryController findById params ,   id : {}   ",id);
        //调用CategoryService实现根据主键查询Category
        Category category = categoryService.findById(id);
        return Result.buildSuccess("查询成功！",category);
    }

    /***
     * 查询Category全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Category",notes = "查询所Category有方法详情",tags = {"CategoryController"})
    @GetMapping
    public Result<List<Category>> findAll(){
        //调用CategoryService实现查询所有Category
        List<Category> list = categoryService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
